package com.firefly.job.quartz;

import com.firefly.job.jobs.JobTest1;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @description: 定时任务管理器，可以修改时间、启动、暂停
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/6/1
 **/
@Component
public class QuartzSchedulerManager {
    @Autowired
    private Scheduler scheduler;

    // 开始执行定时器
    public void startJob() throws SchedulerException {
        startCreateBase(scheduler);
        scheduler.start();
    }
    // 具体任务的配置
    private void startCreateBase(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(JobTest1.class).withIdentity("quartz-base", "g1").build();
        // 基于表达式构建触发器,零时五分执行，0 5 0 * * ?
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("quartz-base", "g1")
                .withSchedule(cronScheduleBuilder).build();
        // 启动服务
        // scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}
